package com.sugar.follow.up.module.business.controller.admin.followupquestionrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.followupquestionrecord.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.followupquestionrecord.FollowUpQuestionRecordDO;
import com.sugar.follow.up.module.business.service.followupquestionrecord.FollowUpQuestionRecordService;

@Tag(name = "管理后台 - 随访发起答题记录")
@RestController
@RequestMapping("/business/follow-up-question-record")
@Validated
public class FollowUpQuestionRecordController {

    @Resource
    private FollowUpQuestionRecordService followUpQuestionRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建随访发起答题记录")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:create')")
    public CommonResult<Long> createFollowUpQuestionRecord(@Valid @RequestBody FollowUpQuestionRecordSaveReqVO createReqVO) {
        return success(followUpQuestionRecordService.createFollowUpQuestionRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新随访发起答题记录")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:update')")
    public CommonResult<Boolean> updateFollowUpQuestionRecord(@Valid @RequestBody FollowUpQuestionRecordSaveReqVO updateReqVO) {
        followUpQuestionRecordService.updateFollowUpQuestionRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除随访发起答题记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:delete')")
    public CommonResult<Boolean> deleteFollowUpQuestionRecord(@RequestParam("id") Long id) {
        followUpQuestionRecordService.deleteFollowUpQuestionRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除随访发起答题记录")
                @PreAuthorize("@ss.hasPermission('business:follow-up-record:delete')")
    public CommonResult<Boolean> deleteFollowUpQuestionRecordList(@RequestParam("ids") List<Long> ids) {
        followUpQuestionRecordService.deleteFollowUpQuestionRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得随访发起答题记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:query')")
    public CommonResult<FollowUpQuestionRecordRespVO> getFollowUpQuestionRecord(@RequestParam("id") Long id) {
        FollowUpQuestionRecordDO followUpQuestionRecord = followUpQuestionRecordService.getFollowUpQuestionRecord(id);
        return success(BeanUtils.toBean(followUpQuestionRecord, FollowUpQuestionRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得随访发起答题记录分页")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:query')")
    public CommonResult<PageResult<FollowUpQuestionRecordRespVO>> getFollowUpQuestionRecordPage(@Valid FollowUpQuestionRecordPageReqVO pageReqVO) {
        PageResult<FollowUpQuestionRecordDO> pageResult = followUpQuestionRecordService.getFollowUpQuestionRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FollowUpQuestionRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出随访发起答题记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFollowUpQuestionRecordExcel(@Valid FollowUpQuestionRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FollowUpQuestionRecordDO> list = followUpQuestionRecordService.getFollowUpQuestionRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "随访发起答题记录.xls", "数据", FollowUpQuestionRecordRespVO.class,
                        BeanUtils.toBean(list, FollowUpQuestionRecordRespVO.class));
    }

}